package com.niit.view;

import com.niit.entity.Users;
import com.niit.service.UsersService;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class UsersView {
    UsersService usersService = new UsersService();

//   使用方法打印登录页面
//    怎么定义一个方法：
//    方法用来做什么？-----打印登录页面
//    方法给谁来访问？----所有人--public
//    方法的返回值类型是什么？---void
//    这个方法需要输入什么参数？--没有
    public void showMainView() throws SQLException {
        Scanner sc = new Scanner(System.in);
        // 打印登录页面
        System.out.println("\n\n");
        System.out.println("+==================================================+");
        System.out.println("|                                                  |");
        System.out.println("|                \u001B[33m当当书店\u001B[0m 管理系统               |");
        System.out.println("|                                                  |");
        System.out.println("+==================================================+");
        System.out.println("|                                                  |");
        System.out.println("|                  主菜单                          |");
        System.out.println("|                                                  |");
        System.out.println("|            \u001B[32m1. 登录\u001B[0m                           |");
        System.out.println("|            \u001B[34m2. 注册\u001B[0m                           |");
        System.out.println("|            \u001B[31m0. 退出\u001B[0m                           |");
        System.out.println("|                                                  |");
        System.out.println("+==================================================+");
        System.out.println("|                                                  |");
        System.out.print("|    请选择操作(0-2): ");
        String num = sc.next();
        switch (num){
            case "1":
                System.out.println("正在跳转到登录页面！");
                showLoginView(sc);
                break;
            case "2":
                System.out.println("正在跳转到注册页面！");
                showRegisterView(sc);
                break;
            case "0":
                System.out.println("正在退出系统....");
                break;
        }

    }

    public void showLoginView(Scanner sc) throws SQLException {
        System.out.println("请输入用户名：");
        String phoneNum = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        boolean login = usersService.login(phoneNum, password);
        if (login){
            System.out.println("登录成功！正在进入系统！");
            displayWelcomePage();
        }else {
            System.out.println("登录失败，请重新登录");
            showLoginView(sc);
        }

    }
    public void showRegisterView(Scanner sc) throws SQLException {
        System.out.println("请输入用户名：");
        String phoneNum = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        boolean register = usersService.register(phoneNum, password);
        if (register){
            System.out.println("正在跳转到登录页面~");
            showLoginView(sc);
        }else {
            System.out.println("请重新注册");
            showRegisterView(sc);
        }
    }

    public  void displayWelcomePage() {
        Scanner scanner = new Scanner(System.in);
        int choice = 0;

        do {
            // 清屏（不一定在所有终端都有效）
            System.out.print("\033[H\033[2J");
            System.out.flush();

            // ANSI颜色代码
            final String RESET = "\u001B[0m";
            final String RED = "\u001B[31m";
            final String GREEN = "\u001B[32m";
            final String YELLOW = "\u001B[33m";
            final String BLUE = "\u001B[34m";
            final String PURPLE = "\u001B[35m";
            final String CYAN = "\u001B[36m";
            final String BOLD = "\u001B[1m";
            final String BG_BLUE = "\u001B[44m";
            final String BG_GREEN = "\u001B[42m";

            // 顶部艺术字标题
            System.out.println(BOLD + BLUE + "╔════════════════════════════════════════════════════════╗" + RESET);
            System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ██████╗  ██████╗  ██████╗ ██╗  ██╗███████╗    " + RESET + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ██╔══██╗██╔═══██╗██╔═══██╗██║ ██╔╝██╔════╝    " + RESET + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ██████╔╝██║   ██║██║   ██║█████╔╝ █████╗      " + RESET + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ██╔══██╗██║   ██║██║   ██║██╔═██╗ ██╔══╝      " + RESET + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ██████╔╝╚██████╔╝╚██████╔╝██║  ██╗███████╗    " + RESET + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "║" + RESET  + PURPLE + "    ╚═════╝  ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝    " + RESET + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "╠════════════════════════════════════════════════════════╣" + RESET);

            // 欢迎信息
            System.out.println(BOLD + BLUE + "║" + RESET + "       " + CYAN + "欢迎使用书香阁图书管理系统 - V2.0" + RESET + "       " + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "║" + RESET + "       " + YELLOW + "探索知识海洋，发现阅读乐趣" + RESET + "       " + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "╠════════════════════════════════════════════════════════╣" + RESET);

            // 主菜单选项
            System.out.println(BOLD + BLUE + "║" + RESET + "  " + GREEN + "1. 图书查询" + RESET + "    " + YELLOW + "2. 图书借阅" + RESET
                    + "    " + CYAN + "3. 图书归还" + RESET + "  " + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "║" + RESET + "  " + PURPLE + "4. 用户管理" + RESET + "    " + RED + "5. 购物车" + RESET
                    + "      " + GREEN + "6. 系统设置" + RESET + "  " + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "║" + RESET + "  "  + BOLD + "0. 退出系统" + RESET + "                              " + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "╠════════════════════════════════════════════════════════╣" + RESET);

            // 系统信息
            System.out.println(BOLD + BLUE + "║" + RESET + "  " + YELLOW + "当前用户: 访客" + RESET + "    " + CYAN + "时间: " + getCurrentTime() + RESET + "  " + BOLD + BLUE + "║" + RESET);
            System.out.println(BOLD + BLUE + "╚════════════════════════════════════════════════════════╝" + RESET);

            // 用户输入
            System.out.print("\n" + BOLD + "请输入您的选择 (0-6): " + RESET);

            try {
                choice = scanner.nextInt();
                scanner.nextLine(); // 消耗换行符

                // 处理用户选择
                switch (choice) {
                    case 1:
                        System.out.println(RED + "\n正在跳转到图书查询系统..." + RESET);
                        Thread.sleep(1000);
                        break;
                    case 2:
                        System.out.println(GREEN + "\n正在跳转到图书借阅系统..." + RESET);
                        Thread.sleep(1000);
                        break;
                    case 3:
                        System.out.println(YELLOW + "\n正在跳转到图书归还系统..." + RESET);
                        Thread.sleep(1000);
                        break;
                    case 4:
                        System.out.println(BLUE + "\n正在跳转到用户管理..." + RESET);
                        showUsersView(scanner);
                        Thread.sleep(1000);
                        break;
                    case 5:
                        System.out.println(PURPLE + "\n正在跳转到购物车..." + RESET);
                        Thread.sleep(1000);
                        break;
                    case 6:
                        System.out.println(CYAN + "\n正在跳转到系统设置..." + RESET);
                        Thread.sleep(1000);
                        break;
                    case 0:
                        System.out.println(RED + "\n感谢使用，再见！" + RESET);
                        break;
                    default:
                        System.out.println(RED + "\n无效输入，请输入0-6之间的数字！" + RESET);
                        Thread.sleep(1000);
                }
            } catch (Exception e) {
                System.out.println(RED + "\n输入错误，请输入数字！" + RESET);
                scanner.nextLine(); // 清除错误输入
                choice = -1;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }
            }

        } while (choice != 0);

        scanner.close();
    }

    public void showUsersView(Scanner scanner) throws SQLException {
        System.out.println("**************************用户管理***************************");
        List<Users> list =  usersService.listUsers(null ,null,null, 1,10);
        System.out.println("用户id\t手机号\t密码\t性别\t生日\t创建时间\t角色");
        for (Users users : list) {
            System.out.println(users);
        }
        System.out.println("请输入数字选择要进行的操作：");
        System.out.println("1.用户查询");
        System.out.println("2.修改用户");
        System.out.println("3.删除用户");
        String next = scanner.next();
        switch (next){
            case "1":
                showUserDetails(scanner);
                break;
            case "2":
                break;
            case "3":
                break;
            case "4":
                break;
            case "5":
                break;
            case "6":
                showMainView();
                break;

        }
    }

    public void showUserDetails(Scanner scanner) throws SQLException {
        System.out.println("**********************查询用户详情***************************");
        System.out.println("请输入要筛选的属性，输入0跳过：");
        System.out.println("输入要过滤的用户名（可以模糊搜索）：");
        String username = scanner.next();
        System.out.println("输入要过滤的性别1男2女：");
        Integer gender = scanner.nextInt();
        System.out.println("输入要过滤角色1,普通用户 2,商家,3.管理员：");
        Integer role = scanner.nextInt();
        System.out.println("页数");
        Integer page = scanner.nextInt();
        System.out.println("每页的条数：");
        Integer pageSize = scanner.nextInt();
        List<Users> users = usersService.listUsers(username, gender, role, page, pageSize);
        System.out.println("用户id\t手机号\t密码\t性别\t生日\t创建时间\t角色");
        for (Users user : users) {
            System.out.println(user);

        }


    }

    private static String getCurrentTime() {
        return java.time.LocalDateTime.now().format(
                java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}
